﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        TicTacToe game;
        PictureBox[] BoxesContainer;

        public Form1()
        {
            normalDifficulty = new ToolStripRadioButtonMenuItem();
            unbeatableDifficulty = new ToolStripRadioButtonMenuItem();
            InitializeComponent();
            normalDifficulty.Checked = true;
            normalDifficulty.CheckOnClick = true;
            normalDifficulty.CheckState = System.Windows.Forms.CheckState.Checked;
            normalDifficulty.Name = "normalDifficulty";
            normalDifficulty.Size = new System.Drawing.Size(152, 22);
            normalDifficulty.Text = "Normalny";
            unbeatableDifficulty.CheckOnClick = true;
            unbeatableDifficulty.Name = "unbeatableDifficulty";
            unbeatableDifficulty.Size = new System.Drawing.Size(152, 22);
            unbeatableDifficulty.Text = "Niezwyciężony";
            BoxesContainer = new PictureBox[9];
            for (int i = 0; i < BoxesContainer.Length; i++)
            {
                BoxesContainer[i] = new PictureBox();
                BoxesContainer[i].Cursor = Cursors.Hand;
                BoxesContainer[i].Name = "box" + (i + 1);
                BoxesContainer[i].Size = new Size(67, 66);
                BoxesContainer[i].TabIndex = i + 1;
                BoxesContainer[i].TabStop = false;
                BoxesContainer[i].Click += new EventHandler(this.box_Click);
                board.Controls.Add(BoxesContainer[i]);
                BoxesContainer[i].BackColor = Color.Transparent;
                BoxesContainer[i].Location = new Point((i / 3) * 66, (i % 3) * 66);
                BoxesContainer[i].SizeMode = PictureBoxSizeMode.CenterImage;
            }
            game = new TicTacToe(Difficulty.Normal);
            timer.Start();
        }

        private void box_Click(object sender, EventArgs e)
        {
            if (timer.Enabled && (string)(sender as PictureBox).Tag != "Clicked")
            {
                (sender as PictureBox).Image = Properties.Resources.cross;
                (sender as PictureBox).Cursor = Cursors.Default;
                (sender as PictureBox).Tag = "Clicked";
                game.PlayerMove((sender as PictureBox).TabIndex - 1);
                CPUMove(game.CalculateCPUMove());
            }
        }

        void CPUMove(int boxIndex)
        {
            BoxesContainer[boxIndex].Image = Properties.Resources.circle;
            BoxesContainer[boxIndex].Cursor = Cursors.Default;
            BoxesContainer[boxIndex].Tag = "Clicked";
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (game.Win('X'))
            {
                label.Text = "Wygrałeś!";
                gameEnd();
            }
            else if (game.Win('O'))
            {
                label.Text = "Przegrałeś.";
                gameEnd();
            }
            else if (game.Draw())
            {
                label.Text = "Remis.";
                gameEnd();
            }
        }

        private void restartButton_Click(object sender, EventArgs e)
        {
            if (normalDifficulty.Checked) game = new TicTacToe(Difficulty.Normal);
            else game = new TicTacToe(Difficulty.Unbeatable);
            label.Text = "";
            restartButton.Enabled = false;
            for (int i = 0; i < BoxesContainer.Length; i++)
            {
                BoxesContainer[i].Image = null;
                BoxesContainer[i].Cursor = Cursors.Hand;
                BoxesContainer[i].Tag = null;
            }
            timer.Start();
            if (radioButton2.Checked)
                CPUMove(game.CalculateCPUMove());
        }

        void gameEnd()
        {
            timer.Stop();
            restartButton.Enabled = true;
            for (int i = 0; i < BoxesContainer.Length; i++)
            {
                BoxesContainer[i].Cursor = Cursors.Default;
            }
        }
    }
}
